package org.impl;


import java.util.ArrayList;
import java.util.List;

import org.dao.Dao;
import org.dbc.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.vo.User;
public class UserDaoImpl implements Dao {                 //数据层，具体实现数据库操作
	
	private Session session = null;
	
	public UserDaoImpl(Session session) {
		// TODO Auto-generated constructor stub
		
		this.session=session;// 取得session
	}

	@Override
	public boolean update(User user) throws Exception{
		// TODO Auto-generated method stub
			User p=(User)session.get(User.class,user.getId());
			p.setName(user.getName());
			p.setSex(user.getSex());
			p.setPassword(user.getPassword());
			session.update(p);
			return true;
	       
		
	}

	@Override
	public void delete(User user) throws Exception{
		// TODO Auto-generated method stub
	      String hql="delete User where id=:id";
	       Query query=session.createQuery(hql);
	          query.setParameter("id", user.getId());
	          query.executeUpdate();
	              
	}

	@Override
	public List<User> search(User user) throws Exception{
		// TODO Auto-generated method stub
		   List<User> list=null;
		 
		   //from后面是对象，不是表名
		   String hql="from User as user where user.id=:id";//使用命名参数，推荐使用，易读。
		   Query query=this.session.createQuery(hql);
		   query.setInteger("id", user.getId());
		   list=query.list();
	       return   list;
	}

	@Override
	public List<User> searchAll() throws Exception{
		// TODO Auto-generated method stub
		List<User> list=null;
		  
		   //from后面是对象，不是表名
		   String hql="from User as user ";//使用命名参数，推荐使用，易读。
		   Query query=this.session.createQuery(hql);
		   list=query.list();
	       return   list;
		
		
	}



	@Override
	public boolean insert(User user) throws Exception {
		// TODO Auto-generated method stub
		session.save(user); // 保存User对象
		
		return true;
	
	}

}
